import Vue from 'vue'
import Vuex from 'vuex';
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		token: "",
		tokenExpired: "",
		uid: "",
		username: "",
		userInfo: {},
		li:123
	},
	mutations: {
		//通用的提交数据的方法
		setData(state, param) {
			for (let i in param) {
				state[i] = param[i];
			}
		},
		logout(state) {
			for (let i in state) {
				if (i == 'userInfo') {
					state[i] = {}
				} else {
					state[i] = '';
				}
			}
		}
	},
	actions: {
		//actions  管不了存储数据的事   他只管分发
		getuserInfo({
			commit,
			state
			// dispatch
		}) {
			//去服务器请求最新的用户信息
			let token = uni.getStorageSync('token');
			uni.request({
				url: 'https://4de16698-d9f4-443a-b416-ef1037cce952.bspapp.com/http/user/getuserInfo',
				method: 'POST',
				data: {
					token
				},
				success: (res) => {
					// console.log(res);
					// this.text = 'request success';
					commit('setData',{
						// token:res.data.token,
						uid:res.data.uid,
						userInfo:res.data.userInfo,
					})
				}
			})
		}
	


	}
})

export default store
